package com.neolix.watch;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.neolix.watch.bluetooth.BlueToothUtil;

import java.util.Timer;
import java.util.TimerTask;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.neolix.watch.util.StringUtil.showText;

public class SearchActivity extends NeolixNaviagationBaseActivity {

    private TextView startTextView;
    private ImageView searchImageView1;
    private ImageView searchImageView2;
    private ImageView searchImageView3;
    private ImageView searchImageView4;
    private ImageView searchImageView5;
    private ImageView searchImageView6;
    private ImageView searchImageView7;

    private boolean isSearching;
    private Timer timer;
    private int currentIndex;


    public SearchActivity() {
        super(R.layout.activity_search);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.search_my_j6));

        initUi();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null)
            timer.cancel();

        if (isSearching)
            BlueToothUtil.syncActive(false);
    }

    private void initListener() {
        startTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!NeolixApplication.getInstance().isConnectWatch()) {
                    showText(R.string.no_connect_watch);
                    return;
                }

                if (isSearching) {
                    stopAnimation();
                } else {
                    //TODO: 发送响铃声音
                    startAnimation();
                }

                setText();
                BlueToothUtil.syncActive(!isSearching);
                isSearching = !isSearching;
            }
        });
    }

    private void initUi() {
        startTextView = (TextView) findViewById(R.id.start_text_view);
        searchImageView1 = (ImageView) findViewById(R.id.search_image_view_1);
        searchImageView2 = (ImageView) findViewById(R.id.search_image_view_2);
        searchImageView3 = (ImageView) findViewById(R.id.search_image_view_3);
        searchImageView4 = (ImageView) findViewById(R.id.search_image_view_4);
        searchImageView5 = (ImageView) findViewById(R.id.search_image_view_5);
        searchImageView6 = (ImageView) findViewById(R.id.search_image_view_6);
        searchImageView7 = (ImageView) findViewById(R.id.search_image_view_7);

        hideAnimationImageView();
    }

    private void stopAnimation() {
        timer.cancel();
        timer = null;
        hideAnimationImageView();
    }

    private void hideAnimationImageView() {
        searchImageView1.setVisibility(INVISIBLE);
        searchImageView2.setVisibility(INVISIBLE);
        searchImageView3.setVisibility(INVISIBLE);
        searchImageView4.setVisibility(INVISIBLE);
        searchImageView5.setVisibility(INVISIBLE);
        searchImageView6.setVisibility(INVISIBLE);
        searchImageView7.setVisibility(INVISIBLE);
    }

    private void startAnimation() {
        currentIndex = 0;
        timer = new Timer();
        timer.schedule(new AnimationTask(), 0, 150);
    }

    private void setText() {
        startTextView.setText(isSearching ? getString(R.string.start) : getString(R.string.stop));
    }

    private class AnimationTask extends TimerTask {
        @Override
        public void run() {
            if (currentIndex == 6)
                currentIndex = 0;
            else
                currentIndex++;
            Message msg = new Message();
            msg.what = 1;
            handler.sendMessage(msg);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 1:
                    changeImageViewVisibility();
                    break;
            }
        }
    };

    private void changeImageViewVisibility() {
        switch (currentIndex) {
            case 0:
                searchImageView1.setVisibility(VISIBLE);
                searchImageView2.setVisibility(INVISIBLE);
                searchImageView3.setVisibility(INVISIBLE);
                searchImageView4.setVisibility(INVISIBLE);
                searchImageView5.setVisibility(INVISIBLE);
                searchImageView6.setVisibility(INVISIBLE);
                searchImageView7.setVisibility(VISIBLE);
                break;
            case 1:
                searchImageView1.setVisibility(VISIBLE);
                searchImageView2.setVisibility(VISIBLE);
                searchImageView3.setVisibility(INVISIBLE);
                searchImageView4.setVisibility(INVISIBLE);
                searchImageView5.setVisibility(INVISIBLE);
                searchImageView6.setVisibility(INVISIBLE);
                searchImageView7.setVisibility(INVISIBLE);
                break;
            case 2:
                searchImageView1.setVisibility(INVISIBLE);
                searchImageView2.setVisibility(VISIBLE);
                searchImageView3.setVisibility(VISIBLE);
                searchImageView4.setVisibility(INVISIBLE);
                searchImageView5.setVisibility(INVISIBLE);
                searchImageView6.setVisibility(INVISIBLE);
                searchImageView7.setVisibility(INVISIBLE);
                break;
            case 3:
                searchImageView1.setVisibility(INVISIBLE);
                searchImageView2.setVisibility(INVISIBLE);
                searchImageView3.setVisibility(VISIBLE);
                searchImageView4.setVisibility(VISIBLE);
                searchImageView5.setVisibility(INVISIBLE);
                searchImageView6.setVisibility(INVISIBLE);
                searchImageView7.setVisibility(INVISIBLE);
                break;
            case 4:
                searchImageView1.setVisibility(INVISIBLE);
                searchImageView2.setVisibility(INVISIBLE);
                searchImageView3.setVisibility(INVISIBLE);
                searchImageView4.setVisibility(VISIBLE);
                searchImageView5.setVisibility(VISIBLE);
                searchImageView6.setVisibility(INVISIBLE);
                searchImageView7.setVisibility(INVISIBLE);
                break;
            case 5:
                searchImageView1.setVisibility(INVISIBLE);
                searchImageView2.setVisibility(INVISIBLE);
                searchImageView3.setVisibility(INVISIBLE);
                searchImageView4.setVisibility(INVISIBLE);
                searchImageView5.setVisibility(VISIBLE);
                searchImageView6.setVisibility(VISIBLE);
                searchImageView7.setVisibility(INVISIBLE);
                break;
            case 6:
                searchImageView1.setVisibility(INVISIBLE);
                searchImageView2.setVisibility(INVISIBLE);
                searchImageView3.setVisibility(INVISIBLE);
                searchImageView4.setVisibility(INVISIBLE);
                searchImageView5.setVisibility(INVISIBLE);
                searchImageView6.setVisibility(VISIBLE);
                searchImageView7.setVisibility(VISIBLE);
                break;
        }
    }
}
